﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NS_QuangCaoTiepThiBUS;
using AjaxControlToolkit;

namespace NS_QuangCaoTiepThi.QCTT.Admin
{
    public partial class QuanLyNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                LayDanhSachChucVu();

                LayDanhSachPhong();

                LayDanhSachLoaiNguoiDung();

                LayDanhSachNguoiDung();

                //tabQuanLyNguoiDung
                LayDanhSachPhongLenComboBox();
                LayDanhSachLoaiNguoiDungLenComboBox();
                LayDanhSachChucVuLenComboBox();
            }
        }

        //Xu ly su kien khi click vao cac tabPanel
        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            //tabQuanLyNguoiDung
            LayDanhSachPhongLenComboBox();
            LayDanhSachLoaiNguoiDungLenComboBox();
            LayDanhSachChucVuLenComboBox();

            LayDanhSachNguoiDung();
        }

        //Table ChucVu
        protected void LayDanhSachChucVu()
        {
            ChucVuBUS cvBUS = new ChucVuBUS();
            gvDanhSachChucVu.DataSource = cvBUS.LayDanhSachChucVu();
            gvDanhSachChucVu.DataBind();
        }
        
        protected void btnThemChucVu_Click(object sender, EventArgs e)
        {
            ChucVu cvDTO = new ChucVu();
            cvDTO.MaChucVu = txtMaChucVu.Text.ToUpper();
            cvDTO.TenChucVu = txtTenChucVu.Text.ToUpper();

            if (txtMaChucVu.Text != string.Empty)
            {
                if (txtTenChucVu.Text != string.Empty)
                {
                    ChucVuBUS cvBUS = new ChucVuBUS();
                    if (cvBUS.KiemTraTonTaiChucVu(cvDTO) == false)
                    {
                        cvBUS.Them(cvDTO);
                    }
                    else
                    {
                        Alert.Show("Da ton tai ma chuc vu nay");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ten chuc vu khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ma chuc vu khong duoc rong");

                return;
            }

            Alert.Show("Da them thanh cong");
            
            LayDanhSachChucVu();
        }

        protected void gvDanhSachChucVu_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachChucVu.EditIndex = e.NewEditIndex;

            LayDanhSachChucVu();
        }

        protected void gvDanhSachChucVu_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtMaChucVu = (TextBox)(gvDanhSachChucVu.Rows[e.RowIndex].Cells[0].FindControl("txtMaChucVu"));
            TextBox txtTenChucVu = (TextBox)(gvDanhSachChucVu.Rows[e.RowIndex].Cells[0].FindControl("txtTenChucVu"));

            ChucVu cvDTO = new ChucVu();
            cvDTO.MaChucVu = txtMaChucVu.Text.ToUpper();
            cvDTO.TenChucVu = txtTenChucVu.Text.ToUpper();

            if (txtTenChucVu.Text != string.Empty)
            {
                ChucVuBUS cvBUS = new ChucVuBUS();
                cvBUS.Sua(cvDTO);
            }
            else
            {
                Alert.Show("Ten chuc vu khong duoc rong");

                return;
            }

            Alert.Show("Da sua thanh cong");
            
            gvDanhSachChucVu.EditIndex = -1;

            LayDanhSachChucVu();
        }

        protected void gvDanhSachChucVu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachChucVu.EditIndex = -1;

            LayDanhSachChucVu();
        }

        protected void gvDanhSachChucVu_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ChucVu cvDTO = new ChucVu();
            cvDTO.MaChucVu = gvDanhSachChucVu.DataKeys[e.RowIndex].Value.ToString();

            ChucVuBUS cvBUS = new ChucVuBUS();
            cvBUS.Xoa(cvDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachChucVu();
        }

        protected void gvDanhSachChucVu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachChucVu.PageIndex = e.NewPageIndex;

            LayDanhSachChucVu();
        }

        //Table Phong
        protected void LayDanhSachPhong()
        {
            PhongBUS pBUS = new PhongBUS();
            gvDanhSachPhong.DataSource = pBUS.LayDanhSachPhong();
            gvDanhSachPhong.DataBind();
        }

        protected void btnThemPhong_Click(object sender, EventArgs e)
        {
            Phong pDTO = new Phong();
            pDTO.MaPhong = txtMaPhong.Text.ToUpper();
            pDTO.TenPhong = txtTenPhong.Text.ToUpper();

            if (txtMaPhong.Text != string.Empty)
            {
                if (txtTenPhong.Text != string.Empty)
                {
                    PhongBUS pBUS = new PhongBUS();
                    if (pBUS.KiemTraTonTaiPhong(pDTO) == false)
                    {
                        pBUS.Them(pDTO);
                    }
                    else
                    {
                        Alert.Show("Da ton tai ma phong nay");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ten phong khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ma phong khong duoc rong");

                return;
            }

            Alert.Show("Da them thanh cong");

            LayDanhSachPhong();
        }

        protected void gvDanhSachPhong_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachPhong.EditIndex = e.NewEditIndex;

            LayDanhSachPhong();
        }

        protected void gvDanhSachPhong_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtMaPhong = (TextBox)(gvDanhSachPhong.Rows[e.RowIndex].Cells[0].FindControl("txtMaPhong"));
            TextBox txtTenPhong = (TextBox)(gvDanhSachPhong.Rows[e.RowIndex].Cells[0].FindControl("txtTenPhong"));

            Phong pDTO = new Phong();
            pDTO.MaPhong = txtMaPhong.Text.ToUpper();
            pDTO.TenPhong = txtTenPhong.Text.ToUpper();

            if (txtTenPhong.Text != string.Empty)
            {
                PhongBUS pBUS = new PhongBUS();
                pBUS.Sua(pDTO);
            }
            else
            {
                Alert.Show("Ten phong khong duoc rong");

                return;
            }

            Alert.Show("Da sua thanh cong");

            gvDanhSachPhong.EditIndex = -1;

            LayDanhSachPhong();
        }

        protected void gvDanhSachPhong_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachPhong.EditIndex = -1;

            LayDanhSachPhong();
        }

        protected void gvDanhSachPhong_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Phong pDTO = new Phong();
            pDTO.MaPhong = gvDanhSachPhong.DataKeys[e.RowIndex].Value.ToString();

            PhongBUS pBUS = new PhongBUS();
            pBUS.Xoa(pDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachPhong();
        }

        protected void gvDanhSachPhong_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachPhong.PageIndex = e.NewPageIndex;

            LayDanhSachPhong();
        }

        //Table LoaiNguoiDung
        protected void LayDanhSachLoaiNguoiDung()
        {
            LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
            gvDanhSachLoaiNguoiDung.DataSource = lndBUS.LayDanhSachLoaiNguoiDung();
            gvDanhSachLoaiNguoiDung.DataBind();
        }

        protected void btnThemLoaiNguoiDung_Click(object sender, EventArgs e)
        {
            LoaiNguoiDung lndDTO = new LoaiNguoiDung();
            lndDTO.MaLoaiNguoiDung = txtMaLoaiNguoiDung.Text.ToUpper();
            lndDTO.TenLoaiNguoiDung = txtTenLoaiNguoiDung.Text.ToUpper();

            if (txtMaLoaiNguoiDung.Text != string.Empty)
            {
                if (txtTenLoaiNguoiDung.Text != string.Empty)
                {
                    LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
                    if (lndBUS.KiemTraTonTaiLoaiNguoiDung(lndDTO) == false)
                    {
                        lndBUS.Them(lndDTO);
                    }
                    else
                    {
                        Alert.Show("Da ton tai ma loai nguoi dung nay");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ten loai nguoi dung khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ma loai nguoi dung khong duoc rong");

                return;
            }

            Alert.Show("Da them thanh cong");

            LayDanhSachLoaiNguoiDung();
        }

        protected void gvDanhSachLoaiNguoiDung_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachLoaiNguoiDung.EditIndex = e.NewEditIndex;

            LayDanhSachLoaiNguoiDung();
        }

        protected void gvDanhSachLoaiNguoiDung_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtMaLoaiNguoiDung = (TextBox)(gvDanhSachLoaiNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtMaLoaiNguoiDung"));
            TextBox txtTenLoaiNguoiDung = (TextBox)(gvDanhSachLoaiNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtTenLoaiNguoiDung"));

            LoaiNguoiDung lndDTO = new LoaiNguoiDung();
            lndDTO.MaLoaiNguoiDung = txtMaLoaiNguoiDung.Text.ToUpper();
            lndDTO.TenLoaiNguoiDung = txtTenLoaiNguoiDung.Text.ToUpper();

            if (txtTenLoaiNguoiDung.Text != string.Empty)
            {
                LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
                lndBUS.Sua(lndDTO);
            }
            else
            {
                Alert.Show("Ten loai nguoi dung khong duoc rong");

                return;
            }

            Alert.Show("Da sua thanh cong");

            gvDanhSachLoaiNguoiDung.EditIndex = -1;

            LayDanhSachLoaiNguoiDung();
        }

        protected void gvDanhSachLoaiNguoiDung_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachLoaiNguoiDung.EditIndex = -1;

            LayDanhSachLoaiNguoiDung();
        }

        protected void gvDanhSachLoaiNguoiDung_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoaiNguoiDung lndDTO = new LoaiNguoiDung();
            lndDTO.MaLoaiNguoiDung = gvDanhSachLoaiNguoiDung.DataKeys[e.RowIndex].Value.ToString();

            LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
            lndBUS.Xoa(lndDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachLoaiNguoiDung();
        }

        protected void gvDanhSachLoaiNguoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachLoaiNguoiDung.PageIndex = e.NewPageIndex;

            LayDanhSachLoaiNguoiDung();
        }

        //Table NguoiDung
        protected void LayDanhSachNguoiDung()
        {
            NguoiDungBUS ndBUS = new NguoiDungBUS();
            gvDanhSachNguoiDung.DataSource = ndBUS.LayDanhSachNguoiDung();
            gvDanhSachNguoiDung.DataBind();
        }

        protected void LayDanhSachPhongLenComboBox()
        {
            PhongBUS pBUS = new PhongBUS();
            cmbMaPhong.DataSource = pBUS.LayDanhSachPhong();
            cmbMaPhong.DataTextField = "TenPhong";
            cmbMaPhong.DataValueField = "MaPhong";
            cmbMaPhong.DataBind();
        }

        protected void LayDanhSachLoaiNguoiDungLenComboBox()
        {
            LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
            cmbMaLoaiNguoiDung.DataSource = lndBUS.LayDanhSachLoaiNguoiDung();
            cmbMaLoaiNguoiDung.DataTextField = "TenLoaiNguoiDung";
            cmbMaLoaiNguoiDung.DataValueField = "MaLoaiNguoiDung";
            cmbMaLoaiNguoiDung.DataBind();
        }

        protected void LayDanhSachChucVuLenComboBox()
        {
            ChucVuBUS cvBUS = new ChucVuBUS();
            cmbMaChucVu.DataSource = cvBUS.LayDanhSachChucVu();
            cmbMaChucVu.DataTextField = "TenChucVu";
            cmbMaChucVu.DataValueField = "MaChucVu";
            cmbMaChucVu.DataBind();
        }

        protected void btnThemNguoiDung_Click(object sender, EventArgs e)
        {
            NguoiDung ndDTO = new NguoiDung();
            ndDTO.MaNguoiDung = txtMaNguoiDung.Text.ToUpper();
            ndDTO.TenNguoiDung = txtTenNguoiDung.Text.ToUpper();
            ndDTO.MatKhau = (new Security()).MaHoaSHA1(txtMatKhau.Text);
            ndDTO.MaPhong = cmbMaPhong.SelectedValue.ToString();
            ndDTO.MaLoaiNguoiDung = cmbMaLoaiNguoiDung.SelectedValue.ToString();
            ndDTO.MaChucVu = cmbMaChucVu.SelectedValue.ToString();

            if (txtMaNguoiDung.Text != string.Empty)
            {
                if (txtTenNguoiDung.Text != string.Empty)
                {
                    if (txtMatKhau.Text != string.Empty)
                    {
                        if (txtMatKhauNhapLai.Text != string.Empty)
                        {
                            if (cmbMaPhong.SelectedValue.ToString() != string.Empty)
                            {
                                if (cmbMaLoaiNguoiDung.SelectedValue.ToString() != string.Empty)
                                {
                                    if (cmbMaChucVu.SelectedValue.ToString() != string.Empty)
                                    {
                                        if (txtMatKhauNhapLai.Text == txtMatKhau.Text)
                                        {
                                            NguoiDungBUS ndBUS = new NguoiDungBUS();
                                            if (ndBUS.KiemTraTonTaiNguoiDung(ndDTO) == false)
                                            {
                                                ndBUS.Them(ndDTO);
                                            }
                                            else
                                            {
                                                Alert.Show("Da ton tai ma loai nguoi dung nay");

                                                return;
                                            }
                                        }
                                        else
                                        {
                                            Alert.Show("Mat khau khong chinh xac");

                                            return;
                                        }
                                    }
                                    else
                                    {
                                        Alert.Show("Ma chuc vu khong duoc rong");

                                        return;
                                    }
                                }
                                else
                                {
                                    Alert.Show("Ma loai nguoi dung khong duoc rong");

                                    return;
                                }
                            }
                            else
                            {
                                Alert.Show("Ma phong khong duoc rong");

                                return;
                            }
                        }
                        else
                        {
                            Alert.Show("Mat khau nhap lai khong duoc rong");

                            return;
                        }
                    }
                    else
                    {
                        Alert.Show("Mat khau khong duoc rong");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ten loai nguoi dung khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ma loai nguoi dung khong duoc rong");

                return;
            }

            Alert.Show("Da them thanh cong");

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachNguoiDung.EditIndex = e.NewEditIndex;

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtTenNguoiDung = (TextBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtTenNguoiDung"));
            TextBox txtMatKhau = (TextBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtMatKhau"));
            TextBox txtMatKhauNhapLai = (TextBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtMatKhauNhapLai"));
            ComboBox cmbMaPhong = (ComboBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("cmbMaPhong"));
            ComboBox cmbMaLoaiNguoiDung = (ComboBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("cmbMaLoaiNguoiDung"));
            ComboBox cmbMaChucVu = (ComboBox)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("cmbMaChucVu"));
            HiddenField hfMatKhau = (HiddenField)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("hfMatKhau"));
            HiddenField hfMatKhauNhapLai = (HiddenField)(gvDanhSachNguoiDung.Rows[e.RowIndex].Cells[0].FindControl("hfMatKhauNhapLai"));

            NguoiDung ndDTO = new NguoiDung();
            ndDTO.MaNguoiDung = gvDanhSachNguoiDung.DataKeys[e.RowIndex].Value.ToString();
            ndDTO.TenNguoiDung = txtTenNguoiDung.Text.ToUpper();
            if (txtMatKhau.Text != string.Empty)
            {
                ndDTO.MatKhau = (new Security()).MaHoaSHA1(txtMatKhau.Text);
            }
            if (txtMatKhau.Text == string.Empty)
            {
                ndDTO.MatKhau = hfMatKhau.Value.ToString();
            }
            ndDTO.MaPhong = cmbMaPhong.SelectedValue.ToString();
            ndDTO.MaLoaiNguoiDung = cmbMaLoaiNguoiDung.SelectedValue.ToString();
            ndDTO.MaChucVu = cmbMaChucVu.SelectedValue.ToString();

            if (txtTenNguoiDung.Text != string.Empty)
            {
                if (cmbMaPhong.SelectedValue.ToString() != string.Empty)
                {
                    if (cmbMaLoaiNguoiDung.SelectedValue.ToString() != string.Empty)
                    {
                        if (cmbMaChucVu.SelectedValue.ToString() != string.Empty)
                        {
                            if (txtMatKhauNhapLai.Text == txtMatKhau.Text)
                            {
                                NguoiDungBUS ndBUS = new NguoiDungBUS();
                                ndBUS.Sua(ndDTO);
                            }
                            else
                            {
                                Alert.Show("Mat khau khong chinh xac");

                                return;
                            }
                        }
                        else
                        {
                            Alert.Show("Ma chuc vu khong duoc rong");

                            return;
                        }
                    }
                    else
                    {
                        Alert.Show("Ma loai nguoi dung khong duoc rong");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ma phong khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ten nguoi dung khong duoc rong");

                return;
            }

            gvDanhSachNguoiDung.EditIndex = -1;

            Alert.Show("Da sua thanh cong");

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachNguoiDung.EditIndex = -1;

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            NguoiDung ndDTO = new NguoiDung();
            ndDTO.MaNguoiDung = gvDanhSachNguoiDung.DataKeys[e.RowIndex].Value.ToString();

            NguoiDungBUS ndBUS = new NguoiDungBUS();
            ndBUS.Xoa(ndDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachNguoiDung.PageIndex = e.NewPageIndex;

            LayDanhSachNguoiDung();
        }

        protected void gvDanhSachNguoiDung_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            ComboBox cmbMaPhong = (ComboBox)(e.Row.FindControl("cmbMaPhong"));
            ComboBox cmbMaLoaiNguoiDung = (ComboBox)(e.Row.FindControl("cmbMaLoaiNguoiDung"));
            ComboBox cmbMaChucVu = (ComboBox)(e.Row.FindControl("cmbMaChucVu"));

            PhongBUS pBUS = new PhongBUS();
            LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
            ChucVuBUS cvBUS = new ChucVuBUS();

            if (cmbMaPhong != null && cmbMaChucVu != null && cmbMaLoaiNguoiDung != null)
            {
                NguoiDung ndDTO = new NguoiDung();
                ndDTO.MaNguoiDung = gvDanhSachNguoiDung.DataKeys[e.Row.RowIndex].Value.ToString();

                NguoiDungBUS ndBUS = new NguoiDungBUS();
                IList<NguoiDung> lstNguoiDung = ndBUS.LayThongTinNguoiDungTheoMaNguoiDung(ndDTO);
                foreach (NguoiDung nd in lstNguoiDung)
                {
                    //Lay du lieu lieu len combobox va sap xep theo MaNguoiDung
                    cmbMaPhong.DataSource = pBUS.LayDanhSachPhong();
                    cmbMaPhong.DataTextField = "TenPhong";
                    cmbMaPhong.DataValueField = "MaPhong";
                    cmbMaPhong.SelectedValue = nd.MaPhong;
                    cmbMaPhong.DataBind();
 
                    cmbMaChucVu.DataSource = cvBUS.LayDanhSachChucVu();
                    cmbMaChucVu.DataTextField = "TenChucVu";
                    cmbMaChucVu.DataValueField = "MaChucVu";
                    cmbMaChucVu.SelectedValue = nd.MaChucVu;
                    cmbMaChucVu.DataBind();

                    cmbMaLoaiNguoiDung.DataSource = lndBUS.LayDanhSachLoaiNguoiDung();
                    cmbMaLoaiNguoiDung.DataTextField = "TenLoaiNguoiDung";
                    cmbMaLoaiNguoiDung.DataValueField = "MaLoaiNguoiDung";
                    cmbMaLoaiNguoiDung.SelectedValue = nd.MaLoaiNguoiDung;
                    cmbMaLoaiNguoiDung.DataBind();
                }
            }
        }
    }
}